package com.yx.changdao.init;

import com.yx.changdao.common.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


/**
 * @Description: <br />
 * 初始化redis缓存链接检查。
 * <p>
 * <br />
 * @Auther: Zhangjiashun
 * @create 2019/11/14 14:08
 */
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class InitCacheRunner implements ApplicationRunner {

    public static final Logger log = LoggerFactory.getLogger(InitCacheRunner.class);

    private RedisUtils redisUtils;

    private final ConfigurableApplicationContext context;

    public InitCacheRunner(RedisUtils redisUtils, ConfigurableApplicationContext context) {
        this.redisUtils = redisUtils;
        this.context = context;
    }

    @Override
    public void run(ApplicationArguments args) {
        try {
            log.info("Redis连接中 ······");
            // 每次启动清空redis缓存
            this.redisUtils.clear();
            log.info("Redis连接成功。");
        } catch (Exception e) {
            log.error("缓存初始化失败，{}", e.getMessage());
            log.error(" ____   __    _   _ ");
            log.error("| |_   / /\\  | | | |");
            log.error("|_|   /_/--\\ |_| |_|__");
            log.error("                        ");
            log.error("启动失败              ");

            this.context.close();
        }
    }
}
